package inc.zhugastrov.dbactions;

import static inc.zhugastrov.ui.UserManagment.getModel;

import java.util.ArrayList;
import java.util.List;
import inc.zhugastrov.sellerform.CurrentOrdersPane;
import inc.zhugastrov.sellerform.Order;
import inc.zhugastrov.sellerform.Basket;
import inc.zhugastrov.sellerform.BasketPanel;
import inc.zhugastrov.sellerform.Item;
import inc.zhugastrov.sellerform.MainPanel;
import inc.zhugastrov.sellerform.OrderIntr;
import inc.zhugastrov.sellerform.Product;
import inc.zhugastrov.sellerform.Wherehouse;
import inc.zhugastrov.users.Account;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class DBActions {
	private static final String PERSISTENCE_UNIT_NAME = "Account";
	private static EntityManagerFactory factory;

	public static boolean addUser(Account newAccount) {
		EntityManager em = factory.createEntityManager();

		// Create new Account
		em.getTransaction().begin();
		em.persist(newAccount);
		em.getTransaction().commit();

		em.close();

		return false;
	}

	public static void getOrdersById(Long id) {
		EntityManager em = factory.createEntityManager();
		List<Order> orders = new ArrayList<Order>();
		for (Object order : em.createNamedQuery("getItems")
				.setParameter("sellerId", id.toString()).getResultList()) {
			orders.add((Order) order);
		}
		for (Order order : orders)
			System.out.println("Hell YEAH");
	}

	public static void cancelOrder(Long id) {
		EntityManager em = factory.createEntityManager();
		Order order = em.find(Order.class, id);

		em.getTransaction().begin();
		em.remove(order);
		em.getTransaction().commit();
	}

	public static void fillOrders() {
		CurrentOrdersPane cop = CurrentOrdersPane.getInstance();
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Order t");
		for (Object order : q.getResultList()) {
			Order newOrder = (Order) order;
			cop.addOrder(newOrder);
		}
	}

	public static void saveOrder(OrderIntr order) {
		EntityManager em = factory.createEntityManager();

		// Create new Account
		em.getTransaction().begin();
		em.persist(order);
		em.getTransaction().commit();

		em.close();
	}

	// updates only items that were modify
	public static void updateDBFromBasket(List<? extends Product> data) {
		EntityManager em = factory.createEntityManager();
		MainPanel mp = MainPanel.getInstance();
		for (Product item : data) {
			Query q = em.createQuery("update Item set amountInStore="
					+ mp.getModel().findWithId(item.getId()).getAmountInStore()
					+ " where id=" + item.getId());
			em.getTransaction().begin();
			q.executeUpdate();
			em.getTransaction().commit();
		}
		em.close();
	}

	public static void clearBasket() {
		EntityManager em = factory.createEntityManager();
		// Read the existing entries and write to console
		Query q = em.createQuery("delete from Basket");
		em.getTransaction().begin();
		q.executeUpdate();
		em.getTransaction().commit();
		em.close();
	}

	public static void addItem(Item item) {
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.persist(item);
		em.getTransaction().commit();
		em.close();
	}

	public static void fillBasket() {
		BasketPanel bp = BasketPanel.getInstance();
		MainPanel mp = MainPanel.getInstance();
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Basket t");
		for (Object item : q.getResultList()) {
			Item newItem = new Item((Basket) item);
			bp.addToBasket(newItem);
			Item toChange = (Item) mp.getModel().findWithId(newItem.getId());
			toChange.setAmountInStore(toChange.getAmountInStore()
					- newItem.getAmountInStore());
			mp.updateUI();

		}
	}

	public static void fillUsers() {
		MainPanel mp = MainPanel.getInstance();
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Account t");
		for (Object acc : q.getResultList()) {
			getModel().addRow((Account) acc);
		}
	}

	public static void fillItems() {
		MainPanel mp = MainPanel.getInstance();
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Item t");
		for (Object item : q.getResultList()) {
			mp.addToItem((Item) item);
		}
	}

	public static void putToWherehouse(Item item) {
		Wherehouse wh = new Wherehouse(item);
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.persist(wh);
		em.getTransaction().commit();
		em.close();
	}
	
	public static void putToBasket(Item item) {
		Basket basket = new Basket(item);
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.persist(basket);
		em.getTransaction().commit();
		em.close();
	}

	public static Long getIdForNewItem(String login) {
		Long id = null;
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t.id from Account t where t.login='"
				+ login + "'");
		if (q.getResultList().size() != 0) {
			id = (Long) q.getSingleResult();
		}
		em.close();
		System.out.println("I get id : " + id);
		return id;
	}

	public static boolean checkForUniqueLogin(String login) {

		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Account t where t.login='"
				+ login + "'");
		if (q.getResultList().size() == 0) {
			em.close();
			return true;

		}
		em.close();
		return false;
	}

	public static void initDB() {
		factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		EntityManager em = factory.createEntityManager();
		em.close();
	}

	public static Account checkUser(String login, String password) {
		Account acc = null;
		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("select t from Account t where t.login='"
				+ login + "' and t.password='" + password + "'");
		if (q.getResultList().size() != 0) {
			acc = (Account) q.getSingleResult();

		}
		em.close();
		return acc;
	}

	public static void deleteItemFromBasket(Long id) {
		EntityManager em = factory.createEntityManager();
		// Read the existing entries and write to console
		Query q = em.createQuery("delete from Basket where id=" + id);
		em.getTransaction().begin();
		q.executeUpdate();
		em.getTransaction().commit();
		em.close();
	}

	public static boolean removeUser(Long id) {

		EntityManager em = factory.createEntityManager();
		// Read the existing entries and write to console
		Query q = em.createQuery("delete from Account where id=" + id);
		em.getTransaction().begin();
		q.executeUpdate();
		em.getTransaction().commit();
		em.close();
		return true;
	}

	public static boolean updateUser(Account account) {

		EntityManager em = factory.createEntityManager();
		Query q = em.createQuery("update Account set address='"
				+ account.getAddress() + "', login='" + account.getLogin()
				+ "', name='" + account.getName() + "', password='"
				+ account.getPassword() + "', salary=" + account.getSalary()
				+ ", passportNumber='" + account.getPassportNumber()
				+ "', surname='" + account.getSurname() + "', position='"
				+ account.getPosition() + "' where id=" + account.getId() + "");
		em.getTransaction().begin();
		q.executeUpdate();
		em.getTransaction().commit();
		em.close();

		return true;
	}

}
